from django.db import models

# Create your models here.
from DjangoUeditor.models import UEditorField
from users.models import UserProfile


class GoodCategory(models.Model):
    name = models.CharField(max_length=20, null=False, verbose_name="识货分类")
    ordered = models.IntegerField(blank=True, null=True, default=0, verbose_name="排序")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="添加时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    update_time = models.DateTimeField(blank=True, null=True, verbose_name="更新时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")

    class Meta:
        db_table = 'good_category'
        verbose_name = '识货分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodArticle(models.Model):
    title = models.CharField(max_length=50, verbose_name="标题")
    category = models.ForeignKey(GoodCategory, models.DO_NOTHING, related_name='good_articles', db_column='category',
                                 blank=True, null=True,
                                 verbose_name="类别")
    author = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name='good_articles', db_column='author',
                               verbose_name="作者")
    image = models.ImageField(max_length=100, null=False, upload_to='good_images/', verbose_name="图片")
    content = UEditorField(imagePath="goods/images/", width=1000, height=300,
                           filePath="goods/files/", default='', verbose_name="内容")
    is_auth = models.NullBooleanField(blank=True, null=True, default=None,
                                      choices=((None, '未审核'), (True, '已通过'), (False, '未通过')), verbose_name="审核")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="发布时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")
    delete_time = models.DateTimeField(blank=True, null=True, verbose_name="删除时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")
    click_count = models.IntegerField(null=True, blank=True, default=0, verbose_name="点击量")
    praise_count = models.IntegerField(null=True, blank=True, default=0, verbose_name="点赞量")
    comment_count = models.IntegerField(null=True, blank=True, default=0, verbose_name="评论量")
    collect_count = models.IntegerField(null=True, blank=True, default=0, verbose_name="收藏量")
    inform_count = models.IntegerField(null=True, blank=True, default=0, verbose_name="举报量")

    class Meta:
        db_table = 'good_article'
        verbose_name = '识货文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class CollectGoodArticle(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name='collect_goods', null=False,
                             verbose_name="收藏者")
    good_article = models.ForeignKey(GoodArticle, models.DO_NOTHING, null=False, verbose_name="识货文章")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="收藏时间")

    class Meta:
        db_table = 'collect_good_article'
        unique_together = (('user', 'good_article'),)
        verbose_name = '收藏识货'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}收藏了{self.good_article.title}'


class CommentGoodArticle(models.Model):
    good_article = models.ForeignKey(GoodArticle, models.DO_NOTHING, related_name='comment_goods', null=False,
                                     verbose_name="识货文章")
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name='comment_goods', null=False,
                             verbose_name="评论者")
    pid = models.ForeignKey('self', related_name="comments", null=True)
    content = models.CharField(max_length=250, null=False, verbose_name="内容")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="评论时间")
    is_delete = models.NullBooleanField(blank=True, null=True, verbose_name="删除")

    class Meta:
        db_table = 'comment_good_article'
        verbose_name = '评论识货'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}评论了{self.good_article.title}'


class InformGoodArticle(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="inform_goods", verbose_name="举报者")
    good_article = models.ForeignKey(GoodArticle, models.DO_NOTHING, related_name="inform_goods", verbose_name="识货文章")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="举报时间")

    class Meta:
        db_table = 'inform_good_article'
        unique_together = (('user', 'good_article'),)
        verbose_name = '举报识货'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}举报了{self.good_article.title}'


class PraiseGoodArticle(models.Model):
    user = models.ForeignKey(UserProfile, models.DO_NOTHING, related_name="praise_goods", null=False,
                             verbose_name="点赞者")
    good_article = models.ForeignKey(GoodArticle, models.DO_NOTHING, related_name="praise_goods", null=False,
                                     verbose_name="识货文章")
    add_time = models.DateTimeField(auto_now_add=True, verbose_name="点赞时间")

    class Meta:
        db_table = 'praise_good_article'
        unique_together = (('user', 'good_article'),)
        verbose_name = '点赞识货'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user.name}点赞了{self.good_article.title}'
